package com.e3.basdoc.warehouse.service.impl;

import java.util.List;

import com.e3.basdoc.warehouse.dao.WarehouseDAO;
import com.e3.basdoc.warehouse.entity.WarehouseEntity;
import com.e3.basdoc.warehouse.model.WarehouseModel;
import com.e3.basdoc.warehouse.model.WarehouseStatus;
import com.e3.basdoc.warehouse.model.WarehouseType;
import com.e3.basdoc.warehouse.service.WarehouseCacheManager;
import com.e3.basdoc.warehouse.service.WarehouseService;
import com.e3.core.base.DAOHelper;
import com.e3.core.base.ServiceException;
import com.e3.core.base.SuperService;

public class WarehouseServiceImpl extends SuperService implements	WarehouseService {
	
	private WarehouseDAO warehouseDAO;
	
	private WarehouseDAO getWarehouseDAO(){
		if(warehouseDAO == null){
			warehouseDAO = DAOHelper.createDAOInstance(WarehouseDAO.class);
		}
		return warehouseDAO;
	}

	public WarehouseModel newWarehouse(WarehouseModel model) throws ServiceException {
		validateWarehouse(model);
		checkWarehouseExisted(model);
		
		WarehouseEntity entity = warehouseModelToEntity(model, null);
		getWarehouseDAO().persist(entity);
		
		model.setId(entity.getId());
		
		WarehouseCacheManager.INSTANCE.put(model);
		
		return model;
	}

	public WarehouseModel modifyWarehouse(WarehouseModel model) throws ServiceException {
		validateWarehouse(model);
		long id = model.getId();
		WarehouseEntity entity = getWarehouseDAO().getById(WarehouseEntity.class, id);
		if (entity == null) {
			return newWarehouse(model);
		}

		String oldCode = entity.getCode();
		if (!oldCode.equals(model.getCode())) {
			checkWarehouseExisted(model);
		}

		warehouseModelToEntity(model, entity);
		getWarehouseDAO().merge(entity);
		
		WarehouseCacheManager.INSTANCE.put(model);
		return model;
	}

	public void deleteWarehouseByIds(List<Long> ids) throws ServiceException {
		if(ids == null||ids.isEmpty()){
			return;
		}
		
		getWarehouseDAO().deleteWarehouseByIds(ids);
		
		WarehouseCacheManager.INSTANCE.remove(ids);
	}

	public void checkWarehouseExisted(WarehouseModel model) throws ServiceException {
		if (model == null) {
			return;
		}

		String code = model.getCode();
		String name = model.getName();
		WarehouseEntity entity = getWarehouseDAO().getWarehouseByCode(code);
		if (entity != null) {
			throw new ServiceException("编码为'" + code + "',名称为'" + name + "'已经存在，请设置新的编码和名称。");
		}

	}

	private void validateWarehouse(WarehouseModel model) throws ServiceException{
		String code = model.getCode();
		String name = model.getName();

		if (code == null || code.trim().length() == 0 
			|| name == null || name.trim().length() == 0) {
			throw new ServiceException("请设置编码和名称。");
		}
	}
	
	private WarehouseEntity warehouseModelToEntity(WarehouseModel model,WarehouseEntity entity){
		entity = ocModelToEntity(WarehouseEntity.class, model, entity);

		entity.setCode(model.getCode());
		entity.setName(model.getName());
		entity.setAdministrator(model.getAdministrator());
		entity.setContractWay(model.getContractWay());
		entity.setStatus(model.getStatus());
		entity.setType(model.getType());
		
		return entity;

	}
}
